home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15157 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  72 lines

  1. Path: gollum.kingston.net!usenet
  2. From: girard@haventree.com (Eugene Girard)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP with a simple C Structure
  5. Date: Wed, 03 Apr 1996 20:33:12 GMT
  6. Organization: HavenTree Software, Limited
  7. Message-ID: <4juqqo$j99@gollum.kingston.net>
  8. References: <4jm38u$j1a@news.bellglobal.com>
  9. NNTP-Posting-Host: buddy.haventree.com
  10. X-Newsreader: Forte Free Agent v0.55
  11.  
  12. bogdanfl@aei.ca (Bogdan  Florescu) wrote:
  13.  
  14. (edited slightly for brevity...)
  15.  
  16. >    Employee Jim, Paul, Pat, Scott;
  17.  
  18. >    Jim.department='a';
  19. >    Paul.department='b';
  20. >    ................................
  21.  
  22. >I need to replace the dots with a program that tells me who else works
  23. >in Paul's department and how much money he makes.
  24.  
  25. >I would like to have the structure variables named with the name of
  26. >the employee, (instead of an array with the name of the employee as a
  27. >member) so I can easily access their members. The structure can be
  28. >modified, but I really need to be able to have expressions such as
  29. >Jim.salary.
  30.  
  31. There is a conceptual problem here.  Basically, the name of the
  32. variable "Jim" is not directly visible at run-time.  The compiler
  33. basically replaces "Jim.whatever" with an actual address in memory,
  34. and thereafter forgets what the developer called the variable.  But
  35. this doesn't even come close to answering your original question...
  36.  
  37. What you might want to do is to build up some association between the
  38. variables and the employees names.  You might want to try something
  39. like:
  40.  
  41. struct lookupTable{
  42.     char *employeeName;
  43.     Employee *data;
  44. } **table = 0;
  45. int tableSize = 0;
  46.  
  47. void RegisterEmployee( char *name, Employee *data)
  48. {
  49.     table = (struct lookupTable **) realloc( 
  50.         table, sizeof( struct lookupTable*) * tableSize);
  51.     table[ tableSize] = (struct lookupTable *) malloc(
  52.         sizeof( struct lookupTable));
  53.     table[ tableSize].employeeName = strdup( name);
  54.     table[ tableSize++].data = data; /* copy of real data.  We don't own
  55. this! */
  56. }
  57.  
  58. Then you can add code like:
  59.     RegisterEmployee( "John", &John);
  60. in your program, and write a fairly straightforward query to find the
  61. information that you originally requested.
  62.  
  63. >Thank you,
  64. >Bogdan Florescu
  65. S'Alright.
  66. Gene
  67. --
  68. Eugene Girard, Programmer, HavenTree Software Limited
  69. HavenTree makes EasyFlow (Windows, DOS, MAC) and Nodemap (Windows, DOS)
  70. For more information, check out http://www.haventree.com
  71.  
  72.